<?php

namespace App\Models\SystemManagement;

use App\Models\CommonModel;
use App\Models\Constant\Fields;
use App\Models\SystemManagement\Privilege;

class Role extends CommonModel
{
    protected $table = 'roles';
    public $primaryKey = 'id';
    protected $guarded = [];

    protected $hidden = ['updated_at','created_at','deleted_at'];

    /**
     * @param $data 表单提交数据
     * @return string 返回响应结果
     * 添加角色数据
     */
    public static function addData($data){
        //保存数据
        $param = new self();
        $re = $param->create($data);
        return $re;
    }

    /**
     * @param $per
     * @return mixed\
     * 获取角色列表 带分页
     */
    public static function getPageData($per){
        return self::orderBy('id', 'desc')->paginate($per);
    }

    /**
     * @return mixed
     * 获取所有角色
     */
    public static function getAll()
    {
        return self::orderBy('id', 'desc')->get();
    }

    /**
     * @param $id
     * @return mixed
     * 获取单条角色数据
     */
    public static function getOne($id){
        $query = self::find($id);
        return $query;
    }

    /**
     * @param $con
     * @return mixed
     * 通过条件查找
     */
    public static function getByCondition($con){
        $query = self::getTableDataByCondition($con,new self());
        return $query;
    }

    /**
     * @param $arr
     * @return bool
     * 修改某条数据
     */
    public static function updateData($arr,$id){
        $query = self::updateTableData($arr,$id,new self());
        return $query;
    }

    /**
     * @param $ids
     * @return \App\Models\Error
     * 删除
     */
    static function del($ids){
        $query = self::batchDelTableData($ids,new self());
        RolePrivilege::whereIn(Fields::ROLE_ID,$ids)->delete();
        return $query;
    }

    /**
     * @param $ids
     * @return \App\Models\Error
     * 恢复
     */
    static function recover($ids){
        $query = self::batchResetTableData($ids,new self());
        return $query;
    }

    /**
     * 获取角色的权限
     * @param $roleId
     * @param array $condition ["menu" => 1, "status" => 1, "type" => 1]
     * @return array
     */
    public static function getRoleRules($roleId, $condition=[]){
        //管理员权限
        if($roleId == self::ADMIN_ROLE_ID){
            return Privilege::orderBy('sort', 'desc')
                        ->where(function($query) use ($condition){
                            if(isset($condition['menu'])) {
                                $query->where('menu',$condition['menu']);
                            }
                            if(isset($condition['status'])) {
                                $query->where('status',$condition['status']);
                            }
                            if(isset($condition['type'])){
                                $query->where('type', $condition['type']);
                            }
                        })
                        ->get();
        }else{
            //角色权限
            return self::find($roleId)
                        ->rolePrivilege()
                        ->orderBy('sort','desc')
                        ->where(function($query) use ($condition){
                            if(isset($condition['menu'])) {
                                $query->where('menu',$condition['menu']);
                            }
                            if(isset($condition['status'])) {
                                $query->where('status',$condition['status']);
                            }
                            if(isset($condition['type'])){
                                $query->where('type', $condition['type']);
                            }
                        })
                        ->get()
                        ->toArray();
        }
        
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * 获取此角色拥有的所有权限
     */
    public function rolePrivilege(){
        return $this->belongsToMany(self::$SYSTEM.'\Privilege','role_privileges',Fields::ROLE_ID,Fields::PRIVI_ID)->orderBy('privilege_id');
    }

    //角色类型
    public function roleCategory()
    {
        return $this->belongsTo(self::$SYSTEM.'\RoleCategory','type',Fields::ID)->select('id','name');
    }
}
